<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加/修改装备</title>
<%@ include file="/view/common/tags.jsp"%>
</head>
<body>
	<div class="well well_p" >
		<sk:form method="post" cssClass="form-horizontal" id="saveAndUpdateFrom" target="_parent" action="">
			<input type="hidden" id="rowId" name="rowId" value='<%=request.getParameter("rowId")%>'/>
			<input type="hidden" id="isReCommit" value="0">
			<input type="hidden" id="token" name="token" value="">
			<input type="hidden" id="gameId" name="gameId" value="">
			<input type="hidden" id="gameName" name="gameName" value="">
			<input type="hidden" id="equipParentId" name="equipParentId" value="">
			<input type="hidden" id="equipParentName" name="equipParentName" value="">
			<input type="hidden" id="equipTypeId" name="equipTypeId" value="">
			<input type="hidden" id="equipType" name="equipType" value="">
			<input type="hidden" id="equipQualityLevelId" name="equipQualityLevelId" value="">
			<input type="hidden" id="equipQualityLevel" name="equipQualityLevel" value="">
			<input type="hidden" id="equipPicUrl" name="equipPicUrl" value="">
			<div class="form-group">
				<label class="col-xs-2 text-right">游戏名称:</label>
				<div class="col-xs-3">
					<sk:select cssClass="form-control select" id="gameIdSelect" name="gameIdSelect" value="" requiredInit="true" ajaxURL="${userInfoV1}/baseGame/gameSelect?wm[DEL_FLAG]=1" onchange="TagUtil.fillOptions('gameIdSelect')">
						<option value="">请选择</option>
					</sk:select>
				</div>
				<label class="col-xs-2 text-right">装备类型:</label>
				<div class="col-xs-3">
					<sk:select cssClass="form-control select" id="equipParentIdSelect" name="equipParentIdSelect" value="" requiredInit="true" parentCascadedSelect="gameIdSelect" ajaxURL="${userInfoV1}/gameGoodsType/typeSelect?wm[TYPE_FLAG]=11" onchange="changeGameEquipType(this.value)">
						<option value="">请选择</option>
					</sk:select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">装备分类:</label>
				<div class="col-xs-3">
					<sk:select cssClass="form-control select" id="equipTypeIdSelect" name="equipTypeIdSelect" value="">
						<option id="equipTypeEle" value="">请选择</option>
					</sk:select>
				</div>
				<label class="col-xs-2 text-right">装备品阶:</label>
				<div class="col-xs-3">
					<sk:select cssClass="form-control select" id="equipQualityLevelIdSelect" name="equipQualityLevelIdSelect" value="" requiredInit="true" parentCascadedSelect="gameIdSelect" ajaxURL="${userInfoV1}/gameEquipLevel/gameEquipLevelSelect?1=1">
						<option value="">请选择</option>
					</sk:select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">装备名称:</label>
				<div class="col-xs-3">
					<input type="text" class="form-control" name="equipName" id="equipName" value=""  placeholder="请输入装备名称">
				</div>
				<label class="col-xs-2 text-right">需求等级:</label>
				<div class="col-xs-3">
					<input type="text" class="form-control" name="equipDemandLevel" id="equipDemandLevel" value=""  placeholder="请输入需求等级">
				</div>
			</div>
            <div class="form-group" >
                <label class="col-xs-2 text-right">是否开启:</label>
                <div class="col-xs-7">
                    <td colspan="2">
                        <label><input name="openFlag" type="radio" checked="checked"  value="1"/>&nbsp;是</label>&nbsp;&nbsp;&nbsp;&nbsp;
                        <label><input name="openFlag" type="radio"  value="0"/>&nbsp;否</label>
                    </td>
                </div>
            </div>
			<div class="form-group" >
				<label class="col-xs-2 text-right">装备图片:</label>
				<div class="cb_box cbx_img_bg col-xs-9">
					<img id="newmainImgPath" src="" width="160px;" height="118px;" onerror="javascript:this.src='${sysManagerV1}/images/uploadImage/noimage.gif';">
					<div class="mainImgPathBox" style="margin-bottom: -56px;">
						<a href="javascript:;" class="file">选择文件<input id="imgPath" type="file" name="Filedata" onchange="newPic(this,'mainImgPath');"/></a>
					</div>
					<div class="exp_box" style="color:red;">建议尺寸为285*169px(jpg、bmp、png等)，并且小于500KB的图片。</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">装备属性：</label>
				<div class="col-xs-7">
					<textarea id="equipProperty" name="equipProperty" style="width:400px;height:70px;border:#ccc solid 1px;text-indent:3px;padding-top:3px;"/></textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">需求属性：</label>
				<div class="col-xs-7">
					<textarea id="equipDemandProperty" name="equipDemandProperty" style="width:400px;height:70px;border:#ccc solid 1px;text-indent:3px;padding-top:3px;"/></textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">装备词缀：</label>
				<div class="col-xs-7">
					<textarea id="equipAffix" name="equipAffix" style="width:400px;height:70px;border:#ccc solid 1px;text-indent:3px;padding-top:3px;"/></textarea>
				</div>
			</div>
		</sk:form>
	</div>
</body>
<%@ include file="/view/common/footer.jsp"%>
<script>
    var gameEquipManageId = $("#rowId").val();//装备ID
    $(function(){
        //添加修改装备时根据ID控制
        if (CheckUtil.isNotNull(gameEquipManageId) && parseInt(gameEquipManageId) > 0) {
            $('#gameIdSelect').attr("disabled", "true");
            $('#equipParentIdSelect').attr("disabled", "true");
            $('#equipTypeIdSelect').attr("disabled", "true");
            $('#equipQualityLevelIdSelect').attr("disabled", "true");
            $('#equipName').attr("readonly", "true");
        }
        //后台登录用户token
        $("#token").val(sessionStorage.getItem("token"));
        //修改装备时获取数据
        if(gameEquipManageId > 0){
            $.ajax({
                url: "${userInfoV1}/gameEquipManage/getGameEquipManageInfo",
                type: "post",
                cache: false,
                data:{"rowId":gameEquipManageId},
                dataType:"json",
                async:false,
                success: function(data) {
                    if (data == null) {
                        return;
                    }
                    $("#gameId").val(data.gameId);
                    $("#gameName").val(data.gameName);
                    $("#gameIdSelect").val(data.gameId);
                    TagUtil.fillOptions('gameIdSelect');
                    $("#equipParentId").val(data.equipParentId);
                    $("#equipParentName").val(data.equipParentName);
                    $("#equipParentIdSelect").val(data.equipParentId);
                    changeGameEquipType(data.equipParentId);
                    $("#equipTypeId").val(data.equipTypeId);
                    $("#equipType").val(data.equipType);
                    $("#equipTypeIdSelect").val(data.equipTypeId);
                    $("#equipQualityLevelId").val(data.equipQualityLevelId);
                    $("#equipQualityLevel").val(data.equipQualityLevel);
                    $("#equipQualityLevelIdSelect").val(data.equipQualityLevelId);
                    $("#equipName").val(data.equipName);
                    if(data.equipPicUrl != ""){
                        $("#newmainImgPath").attr("src", data.equipPicUrl);
                    } else {
                        $("#newmainImgPath").attr("src", "${ctx}/images/uploadImage/noimage.gif");
                    }
                    $("#equipPicUrl").val(data.equipPicUrl);
                    $("#equipProperty").val(data.equipProperty);
                    $("#equipDemandLevel").val(data.equipDemandLevel);
                    $("#equipDemandProperty").val(data.equipDemandProperty);
                    $("#equipAffix").val(data.equipAffix);
                    $("input[name='openFlag']").each(function(){
                        if (data.openFlag == $(this).val()) {
                            $(this).attr("checked",true);
                        } else {
                            $(this).attr("checked",false);
                        }
                    });
                }
            });
        }
    });
    var err = {'1':'添加','2':'修改'};
    var action = {'1': '${userInfoV1}/gameEquipManage/save','2': '${userInfoV1}/gameEquipManage/update'};
    function add(){
        var rowId = $('#rowId').val();
        var opera;
        if (!CheckUtil.isNotNull(rowId) || rowId <= 0) {
            opera = 1;//添加
        } else {
            opera = 2;//修改
        }
        var gameId = $("#gameIdSelect").val();
        if (!CheckUtil.isNotNull(gameId)) {
            layer.msg("请选择游戏!");
            return;
        }
        var equipParentIdSelect = $("#equipParentIdSelect").val();
        if (!CheckUtil.isNotNull(equipParentIdSelect)) {
            layer.msg("请选择装备类型!");
            return;
        }
        var equipTypeIdSelect = $("#equipTypeIdSelect").val();
        if (!CheckUtil.isNotNull(equipTypeIdSelect)) {
            layer.msg("请选择装备分类!");
            return;
        }
        var equipQualityLevelIdSelect = $("#equipQualityLevelIdSelect").val();
        if (!CheckUtil.isNotNull(equipQualityLevelIdSelect)) {
            layer.msg("请选择装备品阶!");
            return;
        }
        var equipName = $("#equipName").val();
        if (!CheckUtil.isNotNull(equipName)) {
            layer.msg("装备名称不能为空!");
            return;
        }
        var imgPath = $("#imgPath").val();//图片
        var equipPicUrl = $("#equipPicUrl").val();
        if (!CheckUtil.isNotNull(imgPath) && !CheckUtil.isNotNull(equipPicUrl)) {
            layer.msg("请选择所要上传的图片!");
            return;
        }
        if (CheckUtil.isNotNull(imgPath) && !CheckUtil.isImg(imgPath)) {
            layer.msg("上传文件只能为图片!");
            return ;
        }
        var equipProperty = $("#equipProperty").val();
        if (!CheckUtil.isNotNull(equipProperty)) {
            layer.msg("装备属性不能为空!");
            return;
        }
        var equipDemandLevel = $("#equipDemandLevel").val();
        if (!CheckUtil.isNotNull(equipDemandLevel) || !CheckUtil.isInt(equipDemandLevel, "+")) {
            layer.msg("请输入正确的需求等级!");
            return;
        }
        var equipDemandProperty = $("#equipDemandProperty").val();
        if (!CheckUtil.isNotNull(equipDemandProperty)) {
            layer.msg("需求属性不能为空!");
            return;
        }
        var equipAffix = $("#equipAffix").val();
        if (!CheckUtil.isNotNull(equipAffix)) {
            layer.msg("装备词缀不能为空!");
            return;
        }
        var openFlag =$('input:radio[name="openFlag"]:checked').val();
        if (!CheckUtil.isNotNull(openFlag)) {
            layer.msg("请选择是否开启!");
            return;
        }
        
        if ($("#isReCommit").val() == 0) {
            $("#isReCommit").val(1);
            //loading层
            var index = layer.load(0, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $("#gameId").val($("#gameIdSelect").val());
            $("#gameName").val($("#gameIdSelect option:selected").text());
            $("#equipParentId").val($("#equipParentIdSelect").val());
            $("#equipParentName").val($("#equipParentIdSelect option:selected").text());
            $("#equipTypeId").val($("#equipTypeIdSelect").val());
            $("#equipType").val($("#equipTypeIdSelect option:selected").text());
            $("#equipQualityLevelId").val($("#equipQualityLevelIdSelect").val());
            $("#equipQualityLevel").val($("#equipQualityLevelIdSelect option:selected").text());
            $.ajax({
                url: action[opera],
                type: "post",
                cache: false,
                data: new FormData($("#saveAndUpdateFrom")[0]),
                dataType:"json",
                processData: false,
                contentType: false,
                async:false,
                success: function(data) {
                    layer.close(index);
                    if(data.code == 0){
                        parent.layer.msg("添加成功！");
                    } else if(data.code == 1){
                        parent.layer.msg("修改成功！");
                    } else if(data.code == "-2"){
                        parent.window.location.href="${sysManagerV1}/";
                    } else {
                        parent.layer.msg(data.message);
                    }
                    if(data.code >= 0){
                        parent.findData();
                        parent.layer.close(parent.tableIndex);
                    }
                }
            });
        }
    }

    //下拉列表显示(根据装备类型查装备分类)
    function changeGameEquipType(parentId) {
        $("#equipTypeEle").siblings().remove();
        if (!CheckUtil.isNotNull(parentId+"")) {
            return false;
        }
        $.ajax({
            type: "post",
            url: "${userInfoV1}/gameGoodsType/smallTypeSelect",
            data:{parentId:parentId,token:sessionStorage.getItem("token")},
            dataType:"json",
            async:false,
            success: function(data) {
                if (data.length<=0) {
                    return;
                }
                var _html = "";
                for (var i=0; i<data.length; i++) {
                    _html += "<option value= '"+data[i].value+"'>"+data[i].label+"</option>";
                }
                $("#equipTypeEle").siblings().remove();
                $("#equipTypeEle").after(_html);
            }
        });
    }
</script>
</html>